package com.allwees.bs.module.oms.service.impl;


import com.allwees.bs.core.modelbase.branch.EBranchType;
import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.oms.dao.AccountDao;
import com.allwees.bs.module.oms.entity.AccountTransit;
import com.allwees.bs.module.oms.entity.FmsMerchantAccount;
import com.allwees.bs.module.oms.entity.FmsRetailerSettleStatement;
import com.allwees.bs.module.oms.entity.OmsOrderItem;
import com.allwees.bs.module.oms.req.query.FmsMerchantAccountQueryReq;
import com.allwees.bs.module.oms.service.AccountService;
import com.allwees.bs.module.oms.service.FmsRetailerSettleStatementService;
import com.allwees.bs.module.oms.service.GeneralOmsOrderItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

@Slf4j
@Service
public class AccountServiceImpl extends BoServiceImpl<AccountDao, FmsMerchantAccount> implements AccountService {

    @Autowired
    private FmsRetailerSettleStatementService fmsRetailerSettleStatementService;

    @Autowired
    private GeneralOmsOrderItemService generalOmsOrderItemService;

    @Override
    public FmsMerchantAccount findDefaultMerchantAccount(EBranchType branchType, String branchUuid) {
        FmsMerchantAccountQueryReq req = new FmsMerchantAccountQueryReq()
                .setBranchType(branchType)
                .setBranchUuid(branchUuid);
        return findUnique(req);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean doWaitingShipAmt(   AccountTransit accountTransit  ){
        FmsMerchantAccount account = findDefaultMerchantAccount(EBranchType.RETAILER, accountTransit.getBranchUuid());
        Assert.notNull(account, "商户[" +  accountTransit.getBranchUuid() + "]结算账户为空");
        account.calculateWaitingShipAmt(accountTransit);
        return  updateByUuid(account);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean doRefundAmtShipped(  AccountTransit accountTransit){
        FmsMerchantAccount account = findDefaultMerchantAccount(EBranchType.RETAILER, accountTransit.getBranchUuid());
        Assert.notNull(account, "商户[" +  accountTransit.getBranchUuid() + "]结算账户为空");
        account.doRefundAmtShipped(accountTransit);
        return updateByUuid(account);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public  boolean doPlatformRefundAmt(AccountTransit accountTransit) {
        FmsMerchantAccount account = findDefaultMerchantAccount(EBranchType.RETAILER, accountTransit.getBranchUuid());
        Assert.notNull(account, "商户[" +  accountTransit.getBranchUuid() + "]结算账户为空");
        account.doPlatformRefundAmt(accountTransit);
        return updateByUuid(account);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public  boolean doRetailerRefundAmt(AccountTransit accountTransit) {
        FmsMerchantAccount account = findDefaultMerchantAccount(EBranchType.RETAILER, accountTransit.getBranchUuid());
        Assert.notNull(account, "商户[" +  accountTransit.getBranchUuid() + "]结算账户为空");
        account.doRetailerRefundAmt(accountTransit);
        return updateByUuid(account);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean doRefundAmtWaitShip(AccountTransit accountTransit) {
        FmsMerchantAccount account = findDefaultMerchantAccount(EBranchType.RETAILER, accountTransit.getBranchUuid());
        Assert.notNull(account, "商户[" +  accountTransit.getBranchUuid() + "]结算账户为空");
        account.doRefundAmtWaitShip(accountTransit);
        return updateByUuid(account);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean doInShippingAmt(  AccountTransit accountTransit) {
        FmsMerchantAccount account = findDefaultMerchantAccount(EBranchType.RETAILER, accountTransit.getBranchUuid());
        Assert.notNull(account, "商户[" +   accountTransit.getBranchUuid() + "]结算账户为空");
        account.calculateInShippingAmt(accountTransit);
       return updateByUuid(account);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean doUnSettle(  AccountTransit accountTransit) {
        FmsMerchantAccount account = findDefaultMerchantAccount(EBranchType.RETAILER, accountTransit.getBranchUuid());
        Assert.notNull(account, "商户[" + accountTransit.getBranchUuid() + "]结算账户为空");
        account.calculateUnSettle(accountTransit);
        return  updateByUuid(account);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean doSettle( AccountTransit accountTransit){
        FmsMerchantAccount account = findDefaultMerchantAccount(EBranchType.RETAILER, accountTransit.getBranchUuid());
        Assert.notNull(account, "商户[" + accountTransit.getBranchUuid() + "]结算账户为空");
        account.calculateSettle(accountTransit);
        OmsOrderItem  omsOrderItem = generalOmsOrderItemService.findByUuid(accountTransit.getOrderItemUuid());
        FmsRetailerSettleStatement fmsRetailerSettleStatement = fmsRetailerSettleStatementService.createSettleStatement(accountTransit.getBranchUuid(), omsOrderItem);
        fmsRetailerSettleStatementService.logSettle(account, fmsRetailerSettleStatement);
        return  updateByUuid(account);
    }


}
